package com.qdsg.ysg.doctor.ui;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.base.BaseActivity;
import com.qdsg.ysg.doctor.ui.adapter.AddMedicineAdapter;
import com.qdsg.ysg.doctor.ui.adapter.SecondAdapter;
import com.qdsg.ysg.doctor.ui.dialog.Finish3Dialog;
import com.qdsg.ysg.doctor.ui.dialog.Item2Dialog;
import com.qdsg.ysg.doctor.ui.dialog.ItemDialog;
import com.qdsg.ysg.doctor.ui.dialog.MedicineDialog;
import com.qdsg.ysg.doctor.util.ToastUtil;
import com.qdsg.ysg.doctor.util.UtilString;
import com.rest.business.RestProxy;
import com.rest.response.BaseResponse;
import com.rest.response.MouldResponse;
import com.rest.response.PrescriptionResponse;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * 增加处方模板界面
 *
 * @time 2020/11/30 17:21
 */

public class AddMouldActivity extends BaseActivity {

    @BindView(R.id.edt_mould_name)
    EditText edt_mould_name;
    @BindView(R.id.edt_advice)
    EditText edt_advice;
    @BindView(R.id.edt_notice)
    EditText edt_notice;
    @BindView(R.id.pre)
    TextView pre;
    @BindView(R.id.main)
    TextView main;
    @BindView(R.id.tail)
    TextView tail;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.btn_yuannei)
    RadioButton btn_yuannei;
    @BindView(R.id.btn_guoyao)
    RadioButton btn_guoyao;
    @BindView(R.id.second_recyclerView)
    RecyclerView second_recyclerView;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.second_add)
    ImageView secondAdd;
    @BindView(R.id.second_diagnose)
    LinearLayout secondDiagnose;
    @BindView(R.id.radio_group)
    RadioGroup radioGroup;
    @BindView(R.id.btn_add)
    ImageView btnAdd;
    @BindView(R.id.btn_confirm)
    TextView btnConfirm;
    @BindView(R.id.btn_del)
    TextView btnDel;

    AddMedicineAdapter adapter;
    SecondAdapter secondAdapter;
    List<PrescriptionResponse.Prescription> list = new ArrayList<>();
    List<PrescriptionResponse.Second> list2 = new ArrayList<>();

    String preAppendId, icdId, tailAppendId;
    Boolean isGuoYao = false;
    MouldResponse.Mould mould;
    int type;
    Finish3Dialog mFinish3Dialog;

    @Override
    protected int getLayoutResource() {
        return R.layout.aty_prescription_mould;
    }

    @Override
    protected void initData() {
        mould = (MouldResponse.Mould) getIntent().getSerializableExtra("obj");
        if (mould != null) {
            getInfo();
        }
    }

    @Override
    protected void initViews() {
        tvTitle.setText("处方模板");
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        adapter = new AddMedicineAdapter(mContext, list);
        recyclerView.setAdapter(adapter);
        LinearLayoutManager manager2 = new LinearLayoutManager(this);
        manager2.setOrientation(LinearLayoutManager.VERTICAL);
        second_recyclerView.setLayoutManager(manager2);
        secondAdapter = new SecondAdapter(this, list2, true, isGuoYao);
        second_recyclerView.setAdapter(secondAdapter);
    }

    @OnClick({R.id.btn_guoyao, R.id.btn_yuannei, R.id.btn_add, R.id.second_add, R.id.pre, R.id.tail, R.id.main, R.id.btn_del, R.id.btn_confirm, R.id.iv_delete})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_guoyao:
                isGuoYao = true;
                if (type == 0) {
                    if (list.size() > 0) {
                        showFinish3Dialog(0);
                    }
                }
                break;
            case R.id.btn_yuannei:
                isGuoYao = false;
                if (type == 1) {
                    if (list.size() > 0) {
                        showFinish3Dialog(1);
                    }
                }
                break;
            case R.id.btn_add:
                if (btn_yuannei.isChecked()) {
                    showSelectHeadPicDialog(0);
                }
                if (btn_guoyao.isChecked()) {
                    showSelectHeadPicDialog(1);
                }
                break;
            case R.id.second_add: //添加次诊断
                if (UtilString.isEmpty(pre.getText().toString().trim())) {
                    ToastUtil.showToast(mContext, "请填写主诊断前缀");
                    return;
                }
                if (UtilString.isEmpty(main.getText().toString().trim())) {
                    ToastUtil.showToast(mContext, "请填写主诊断");
                    return;
                }
//                if (UtilString.isEmpty( tvSuffix.getText().toString().trim() )) {
//                    Toast.makeText( mContext, "请填写主诊断后缀", Toast.LENGTH_SHORT ).show();
//                    return;
//                }
                list2.add(new PrescriptionResponse.Second());
                secondAdapter.notifyDataSetChanged();
                break;

            case R.id.pre:
                showDialog(2, 0);
                break;
            case R.id.tail:
                showDialog(4, 0);
                break;
            case R.id.main:
                showSelectHeadPicDialog2(3, 0);
                break;
            case R.id.btn_del:
                delMould();
                break;
            case R.id.btn_confirm:
                save();
                break;
            case R.id.iv_delete:
                tail.setText("");
                tailAppendId = "";
                break;
            default:
                break;
        }
    }


    private void showFinish3Dialog(final int type) {
        mFinish3Dialog = Finish3Dialog.newInstance(type);
        mFinish3Dialog.show(getSupportFragmentManager(), "Finish3Dialog");
        mFinish3Dialog.setmListener(new Finish3Dialog.OnItemClickListener() {

            @Override
            public void onConfirm() {
                list.clear();
                adapter.notifyDataSetChanged();
                mFinish3Dialog.dismiss();
                if (type == 0) {
                    AddMouldActivity.this.type = 1;
                } else {
                    AddMouldActivity.this.type = 0;
                }
            }

            @Override
            public void onCancel() {
                if (type == 0) {
                    btn_yuannei.setChecked(true);
                } else {
                    btn_guoyao.setChecked(true);
                }
                mFinish3Dialog.dismiss();
            }
        });
    }

    /**
     * 前缀或者后缀弹框
     *
     * @time 2020/11/30 17:37
     */
    private void showDialog(final int type, final int position) {
        // 初始化点击头像弹出的对话框
        ItemDialog mItemDialog = ItemDialog.newInstance(type, "", isGuoYao);
        mItemDialog.setOnItemClickListener((s, id) -> {
            if (type == 2) {
                pre.setText(s);
                preAppendId = id;
            } else if (type == 4) {
                tail.setText(s);
                tailAppendId = id;
            }
        });
        mItemDialog.show(getSupportFragmentManager(), "mItemDialog");
    }


    private void showSelectHeadPicDialog2(final int type, final int position) {
        // 初始化点击头像弹出的对话框
        Item2Dialog mItem2Dialog = Item2Dialog.newInstance(type);
        mItem2Dialog.setOnItemClickListener((s, id) -> {
            main.setText(s);
            icdId = id;
        });
        mItem2Dialog.show(getSupportFragmentManager(), "mItemDialog");
    }


    @SuppressLint("NotifyDataSetChanged")
    private void showSelectHeadPicDialog(int type) {
        // 初始化点击头像弹出的对话框
        MedicineDialog mMedicineDialog = MedicineDialog.newInstance(type);
        mMedicineDialog.setOnItemClickListener(p -> {
            list.add(p);
            adapter.notifyDataSetChanged();

        });
        mMedicineDialog.show(getSupportFragmentManager(), "mMedicineDialog");
    }

    private void save() {
        String id = "";
        if (mould != null) {
            id = mould.id;
        }
        if (btn_yuannei.isChecked()) {
            type = 0;
        }
        if (btn_guoyao.isChecked()) {
            type = 1;
        }
        if (TextUtils.isEmpty(icdId)) {
            ToastUtil.showToast(mContext, "主诊断不能为空");
            return;
        }
        RestProxy.getInstance().savePrescriptionModel(id, edt_mould_name.getText().toString(), edt_advice.getText().toString(), edt_notice.getText().toString(), preAppendId, icdId, tailAppendId, type + "", list, list2, new Observer<BaseResponse>() {
            @Override
            public void onSubscribe(Disposable d) {
                showProgressDialog(mContext);
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                finish();
            }

            @Override
            public void onError(Throwable e) {
                dismissProgressDialog();
                ToastUtil.onError(mContext, e);
            }

            @Override
            public void onComplete() {
                dismissProgressDialog();
            }
        });
    }

    private void getInfo() {
        RestProxy.getInstance().getPrescriptionModel(mould.id, new Observer<MouldResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(MouldResponse mouldResponse) {
                type = mouldResponse.data.type;
                if (mouldResponse.data.type == 0) {
                    btn_yuannei.setChecked(true);
                } else {
                    btn_guoyao.setChecked(true);
                }
                edt_mould_name.setText(mouldResponse.data.name);
                edt_advice.setText(mouldResponse.data.diagnoseResult);
                edt_notice.setText(mouldResponse.data.remark);
                list.addAll(mouldResponse.data.westernList);
                adapter.notifyDataSetChanged();
                if (null != mouldResponse.data.secondList) {
                    list2.addAll(mouldResponse.data.secondList);
                }
                secondAdapter.notifyDataSetChanged();
                pre.setText(mouldResponse.data.preAppend);
                main.setText(mouldResponse.data.icd);
                tail.setText(mouldResponse.data.tailAppend);
                icdId = mouldResponse.data.icdId;
                preAppendId = mouldResponse.data.preAppendId;
                tailAppendId = mouldResponse.data.tailAppendId;

            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError(mContext, e);
            }

            @Override
            public void onComplete() {

            }
        });
    }

    private void delMould() {
        if (mould == null) {
            return;
        }
        RestProxy.getInstance().delMould(mould.id, new Observer<BaseResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                finish();
            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError(mContext, e);
            }

            @Override
            public void onComplete() {

            }
        });
    }
}
